package com.SweetWhite.UserManagement.model.controller;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.SweetWhite.UserManagement.model.bean.Hero_User;
import com.SweetWhite.UserManagement.model.service.InsertService;
import com.SweetWhite.UserManagement.model.service.SelectService;



@Controller
public class InsertController extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private InsertService service;
	@Autowired
	private SelectService s_service;
	@RequestMapping("checkInsert")
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 req.setCharacterEncoding("UTF-8");
		 resp.setCharacterEncoding("UTF-8");
		 int No=Integer.parseInt(req.getParameter("No"));
		 PrintWriter pw =resp.getWriter();
		 try {
			if ( No==s_service.getHero_UserByNo(No).getNo()) {
				pw.print("不可以哟，这个位置有人了");
			}
		} catch (Exception e) {
			// TODO: handle exception
			pw.print("还没有人在这个位置，放心加入吧");
		}finally {
			pw.flush();
			pw.close();
		}
	}
	@RequestMapping("insert.action")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		   req.setCharacterEncoding("UTF-8");
		   resp.setCharacterEncoding("UTF-8");
		Hero_User hu=new Hero_User();
		PrintWriter pw=resp.getWriter();
		int n=Integer.parseInt(req.getParameter("No"));
		hu.setNo(n);
		hu.setNickName(req.getParameter("NickName"));
		hu.setRealName(req.getParameter("RealName"));
		hu.setSex(req.getParameter("Sex"));
		hu.setShowTime(req.getParameter("ShowTime"));
		hu.setPassword(req.getParameter("password"));
		try {
			service.setHero_User(hu);
			pw.print("添加成功");
		} catch (Exception e) {
			// TODO: handle exception
			pw.print("添加失败");
		}finally {
			pw.flush();
			pw.close();
		}
		


	}

}